if (CLIENT) then
	language.Add("Tool_movingcam_name", "Moving Camera Tool 2")
	language.Add("Tool_movingcam_desc", "For the machinima guys :3")
	language.Add("Tool_movingcam_0", "Left-click to make a camera point, right-click to make the manager")
	
	language.Add("Undone_Camera Point", "Camera Point Undone!")
	language.Add("Undone_Camera Manager", "Camera Manager Undone!")
end

TOOL.Category = "Render"
TOOL.Name = "Moving Camera"
TOOL.Command = nil
TOOL.ConfigName = ""

local Buttons = {}
Buttons[0] = "1"
Buttons[1] = "2"
Buttons[2] = "3"
Buttons[3] = "4"
Buttons[4] = "5"
Buttons[5] = "6"
Buttons[6] = "7"
Buttons[7] = "8"
Buttons[8] = "9"
Buttons[9] = "0"

TOOL.Info = {}
TOOL.CamMans = {}

for i, v in pairs(Buttons) do
	TOOL.Info[i] = {}
	TOOL.Info[i].CamEnts = {}
	TOOL.Info[i].Cameras = 1
	TOOL.CamMans[i] = nil
end

TOOL.ClientConVar["time"] = 15
TOOL.ClientConVar["lookforward"] = 0
TOOL.ClientConVar["key"] = "5"

function TOOL:CreateOrUpdateManager()
	local key = self:GetClientNumber("key")
	
	table.insert(self.Info[key].CamEnts, ent)
	self.Info[key].Cameras = self.Info[key].Cameras + 1
	
	if(!self.CamMans[key]) then
		self.CamMans[key] = ents.Create("sent_camera_manager")
		local CamMan = self.CamMans[key]
		
		CamMan:SetPos(self:GetOwner():GetShootPos())
		CamMan.Owner = self:GetOwner()
		CamMan:Spawn()
		CamMan:Activate()
		
		numpad.OnDown(self:GetOwner(), self:GetClientNumber("key"), "MovingCamera_Toggle", CamMan)
	else
	end
	
	local CamMan = self.CamMans[key]
	CamMan.StepDuration = self:GetClientNumber("time")
	CamMan.Cams = self.Info[key].CamEnts
end

function TOOL:LeftClick(trace)
	if(CLIENT) then return true end
	
	local SpawnPos = self:GetOwner():GetShootPos()
	
	ent = ents.Create("sent_camera_point")
	ent:SetPos(SpawnPos)
	ent:SetAngles(self:GetOwner():GetAngles())
	ent:Spawn()
	ent:Activate()
	
	undo.Create("Camera Point")
	undo.AddEntity(ent)
	undo.SetPlayer(self:GetOwner())
	undo.Finish()
	
	self:CreateOrUpdateManager()
	
	return true
end

function TOOL:RightClick(trace)
	if(CLIENT) then return true end
	
	local SpawnPos = self:GetOwner():GetShootPos()
	
	ent = ents.Create("sent_camera_point")
	ent:SetPos(SpawnPos)
	ent:SetAngles(self:GetOwner():GetAngles())
	ent:Spawn()
	ent:Activate()
	
	undo.Create("Camera Point")
	undo.AddEntity(ent)
	undo.SetPlayer(self:GetOwner())
	undo.Finish()
	
	self:CreateOrUpdateManager()
	
	return true
end

function TOOL:Reload(trace)
end

function TOOL.BuildCPanel(panel)
	panel:AddControl("Header", {
		Text = "Moving Camera Tool",
		Description = "A tool that allows you to create moving cameras"
	})
	
	panel:AddControl("Slider", {
		Label = "Time",
		Type = "Float",
		Min = "0",
		Max = "100",
		Command = "movingcam_time"
	
	})
	
	panel:AddControl("CheckBox", {
		Label = "Look Along Path",
		Description = "Should the camera face the direction it's moving?",
		Command = "movingcam_lookforward"
	})
end
